import 'package:demo/event/event_setting.dart';
import 'package:demo/views/base/base_controller.dart';
import 'package:demo/tools/tools_enum.dart';
import 'package:demo/tools/tools_sqlite.dart';
import 'package:demo/tools/tools_storage.dart';
import 'package:demo/component/component_contact.dart';

class FriendIndexController extends BaseController {
  // 当前用户
  String userId = ToolsStorage().local().userId;
  // 联系人列表
  List<ContactModel> dataList = [];
  // 刷新
  getList() async {
    // 执行
    List<ChatFriend> friendList = await ToolsSqlite().friend.getList();
    // 清空
    dataList.clear();
    // 转换
    for (var friend in friendList) {
      dataList.add(
        ContactModel(
          userId: friend.userId,
          nickname: friend.nickname,
          portrait: friend.portrait,
          remark: friend.remark,
          extend: 'ID：${friend.userNo}',
        ),
      );
    }
    update();
  }

  @override
  void onInit() {
    super.onInit();
    // 刷新好友
    getList();
    // 监听好友
    subscription = EventSetting().event.stream.listen((model) {
      // 好友
      if (SettingType.friend == model.setting) {
        // 刷新好友
        getList();
      }
    });
  }
}
